package org.cmcc.pcap.voltePcapParse.utils.pcap;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * 数据工具类
 * @author starfish
 *
 */
public class DataUtils {

	private DataUtils() {}

	/**
	 * 将一维的字节数组逆序
	 * @param arr
	 */
	public static void reverseByteArray(byte[] arr){
		byte temp;
		int n = arr.length;
		for(int i = 0; i < n / 2; i++){
			temp = arr[i];
			arr[i] = arr[n - 1 - i];
			arr[n - 1 - i] = temp;
		}
	}

	/**
	 * byte 转 int
	 * @param b
	 * @return
	 */
	public static int byteToInt (byte b) {
		return (b & 0xff);
	}

	/**
	 * 一维字节数组转 int 值(4 字节)
	 * @param b
	 * @return
	 */
	public static int byteArrayToInt(byte[] b){
		return byteArrayToInt(b, 0);
	}

	/**
	 * 一维字节数组转 int 值(4 字节)
	 * @param b
	 * @param offset
	 * @return
	 */
	public static int byteArrayToInt(byte[] bytes, int offset){
		int value= 0;
		//由高位到低位
		for (int i = 0; i < 4; i++) {
			int shift= (4 - 1 - i) * 8;
			value +=(bytes[i] & 0x000000FF) << shift;//往高位游
		}

		return value;
	}

	/**
	 * 一维字节数组转 short 值(2 字节)
	 * @param b
	 * @return
	 */
	public static short byteArrayToShort(byte[] b){
		return byteArrayToShort(b, 0);
	}

	/**
	 * 一维字节数组转 short 值(2 字节)
	 * @param b
	 * @param offset
	 * @return
	 */
	public static short byteArrayToShort(byte[] b,int offset){
		return (short) (((b[offset] & 0xff) << 8) | (b[offset + 1] & 0xff)); 
	}

	/**
	 * byte 转为 16 进制字符串
	 * @param b
	 * @return
	 */
	public static String byteToHexString (byte b) {
		return intToHexString(byteToInt(b));
	}
	
	public static String byteToHexString2 (byte b) {
		String hex = intToHexString(byteToInt(b));
		int len = hex.length();
		if (len < 2) {	// 若小于 4，则高位补 0
			int n = 2 - len;
			for (int i = 0; i < n; i ++) {
				hex = "0" + hex;
			}
		}
		return "0x" + hex;
	}
	
	public static String bytesToHexString(byte[] src){
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return "0x" + stringBuilder.toString();
    }
	
	public static String bytesToHexString2(byte[] src){
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }

	/**
	 * 将 int 类型数据转为 byte[] 
	 * @param data
	 * @return
	 */
	public static byte[] intToByteArray(int i) {
		byte[] result = new byte[4];   
		//由高位到低位
		result[0] = (byte)((i >> 24) & 0xFF);
		result[1] = (byte)((i >> 16) & 0xFF);
		result[2] = (byte)((i >> 8) & 0xFF); 
		result[3] = (byte)(i & 0xFF);
		return result;
	}

	/**
	 * short 转 16 进制字符串
	 * @param s
	 * @return
	 */
	public static String shortToHexString4 (short s) {
		String hex = intToHexString(s);
		int len = hex.length();
		if (len > 4) {	// 此时 short 值为负值，高位会补 1，变成 ffffed5c，因此截去符号位
			hex = hex.substring(4);
		} 

		len = hex.length();
		if (len < 4) {	// 若小于 4，则高位补 0
			int n = 4 - len;
			for (int i = 0; i < n; i ++) {
				hex = "0" + hex;
			}
		}

		return "0x" + hex;
	}

	public static String shortToHexString2(short s) {
		String hex = intToHexString(s);
		int len = hex.length();
		if (len > 2) {	// 此时 short 值为负值，高位会补 1，变成 ffffed5c，因此截去符号位
			hex = hex.substring(2);
		} 
		len = hex.length();
		if (len < 2) {	// 若小于 4，则高位补 0
			int n = 2 - len;
			for (int i = 0; i < n; i ++) {
				hex = "0" + hex;
			}
		}
		return "0x" + hex;
	}
	public static String stringToHex(String hex) {
//		String hex = intToHexString(s);
		int len = hex.length();
		if (len > 4) {	// 此时 short 值为负值，高位会补 1，变成 ffffed5c，因此截去符号位
			hex = hex.substring(4);
		} 

		len = hex.length();
		if (len < 4) {	// 若小于 4，则高位补 0
			int n = 4 - len;
			for (int i = 0; i < n; i ++) {
				hex = "0" + hex;
			}
		}
		return "0x" + hex;
	}

	/**
	 * 将 int 转为 16 进制字符串
	 * @param data
	 * @return
	 */
	public static String intToHexString (int data) {
		return Integer.toHexString(data);
	}

	/**
	 * 将二进制的数字字符串转为十进制
	 * @param str
	 */
	public static int binaryToDecimal (String str) {
		String[] strs = str.split("");
		List<Integer> datas = new ArrayList<Integer>();
		for (String s : strs) {
			datas.add(Integer.valueOf(s));
		}
		int size = datas.size();

		int values = 0;
		if (size <= 16) {
			for (int i = 0; i < size; i ++) {
				values += (datas.get(i) * ((int) Math.pow(2, size - i - 1)));
			}
		} else {	// 这种情况是该数值为负值，前面补 1
			// 只留下最后 16 位数
			int offset = size - 16;
			for (int i = 0; i < 16; i ++) {
				values += (datas.get(i + offset) * ((int) Math.pow(2, 16 - i - 1)));
			}
		}

		return values;
	}
	
	/**
	 * 判断参数是否为整数
	 * @param arg
	 * @return
	 */
	public static Boolean isNumeric(float arg) {
		int i=(int)arg;
		if(i==arg && i!=0){
			return true;	
		}
		else{
			return false;	
		}
	}
	 /** 
     * 将byte[]转为各种进制的字符串 
     * @param bytes byte[] 
     * @param radix 基数可以转换进制的范围，从Character.MIN_RADIX到Character.MAX_RADIX，超出范围后变为10进制 
     * @return 转换后的字符串 
     */  
	public static String binary(byte[] bytes, int radix){  
        return new BigInteger(1, bytes).toString(radix);// 这里的1代表正数  
    }  
	public static void main(String[] args) {
//		byte[]  buff_2 =new byte[1];
		byte buff_2 	= (byte) -4;
		byteToHexString2(buff_2);
//		Integer x = Integer.parseInt(byteToHexString(buff_2),16);
		System.out.println((int)buff_2);
	}
	
	public static byte[] shortToByte2(short number) { 
        int temp = number; 
        byte[] b = new byte[2]; 
        for (int i = (b.length - 1); i >= 0; i--) { 
            b[i] = new Integer(temp & 0xff).byteValue();// 将最低位保存在最高位 
            temp = temp >> 8; // 向右移8位 
        } 
        return b; 
    } 
}